package com.jade.explorer.ui;


import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;

import com.jade.explorer.dirscan.FileSystem;
import com.jade.explorer.views.JadeSharedImages;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;

/**
 * This class allows to create message dialogs for new files creation
 * @author Alexander Sokol
 * @created 18.08.2012
 */
public class NewFileDialog extends Dialog {
	
	//------------------------------Private fields
	/** Text box */
	private Text _text;
	
	/** Dialog title */
	private String _title;
	
	/** Dialog message */
	private String _message;
	
	/** Extension of new file */
	private String _extension;
	
	/** entered file name + extension, empty if name has forbidden chars */
	private String _result;
	
	/** filename entered in text box */
	private String _temp = "";
	
	/** Dialog big picture */
	private Image _pic = JadeSharedImages.getBigCustomize();
	
	
	//------------------------------Protected methods
	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);
		
		Canvas canvas = new Canvas(container, SWT.NO_REDRAW_RESIZE);
		canvas.setBounds(10, 64, 64, 64);
		canvas.setBackgroundImage(_pic);
		
		Label messageLabel = new Label(container, SWT.NONE);
		messageLabel.setBounds(101, 75, 106, 15);
		messageLabel.setText(_message);
		
		_text = new Text(container, SWT.BORDER);
		_text.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				_temp = _text.getText();
			}
		});
		_text.setBounds(101, 96, 400, 21);
		
		Composite titleFrame = new Composite(container, SWT.NONE);
		titleFrame.setBounds(0, 0, 444, 44);
		titleFrame.setBackgroundImage(JadeSharedImages.getMenuTitleImg());
		
		titleFrame.setFont(SWTResourceManager.getFont("Rockwell", 14, SWT.BOLD));
		
		titleFrame.addPaintListener(new PaintListener() { 
			public void paintControl(PaintEvent e) { 
				e.gc.drawString(_title, 10, 10, true); 
				} 
		}); 
		
		return container;
	}
	
	
	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		final Shell shell = super.getShell();
		Button buttonOK = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,true);
		buttonOK.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (FileSystem.checkInput(_temp)){
					if (!_temp.equals("")){
						_result = _temp + _extension;
						close();
					}
					else{
						MessageDialog.openError(shell, "Error", "Enter file name!");
						open();
					}
					close();
				}
				else{
					MessageDialog.openError(shell, "Error", "File name cant contain any of following characters:\n< > / \\ * : ; ? \" ' | .");
					_result = "";
					open();
				}
			}
		});
		Button buttonCancel = createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
		buttonCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_result = "";
				close();
			}
		});
		
	}
	
	public String getResult(){
		return _result;
	}
	
	
	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 230);
	}
	
	
	//------------------------------Constructors
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public NewFileDialog(Shell parentShell, String title, String message, String extention) {
		super(parentShell);
		_title = title;
		_message = message;
		_extension = extention;
	}
	
	
	//------------------------------Public methods
	/**
	 * Sets big customize picture into dialog
	 */
	public void setCustomizePicture(){
		_pic = JadeSharedImages.getBigCustomize();
	}
	
	
	/**
	 * Sets big new folder picture into dialog
	 */
	public void setFolderPlusImage(){
		JadeSharedImages.getBigNewFolder();
	}
}

	


